package com.xique.park.service.biz.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.park.bean.domain.ParkGarage;
import com.xique.park.bean.domain.ParkOn;
import com.xique.park.service.biz.service.IParkSpaceBizService;
import com.xique.park.service.service.IParkGarageService;
import com.xique.park.service.service.IParkOnService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 剩余车位相关方法
 *
 * @author xique
 */
@Service
public class ParkSpaceBizServiceImpl implements IParkSpaceBizService {

    /**
     * 可以通行，计入车位统计的记录状态
     */
    private String passStatus = "0,2,11,21,31,42,44,52,54,62,64,65,66,72,74,75,82,84,85,92,94,102,104,112,114,115,116,122,124,125,132,134,210,211,212,213,220";

    @Autowired
    private IParkGarageService parkGarageService;

    @Autowired
    private IParkOnService parkOnService;

    /**
     * 计算整个车场的总车位数和剩余车位数
     */
    @Override
    public Map<String, Long> getParkSpaces(Long parkId) {
        Map<String, Long> map = new HashMap<>();
        ParkGarage parkGarage = new ParkGarage();
        parkGarage.setParkId(parkId);
        List<ParkGarage> garageList = parkGarageService.selectParkGarageList(SecurityUtils.getParkId(), parkGarage);
        Long totalSpaces = 0L;
        Long remainSpaces = 0L;
        if (CollectionUtils.isNotEmpty(garageList)) {
            for (ParkGarage garage : garageList) {
                totalSpaces += getGarageTotalSpaces(garage);
                remainSpaces += getGarageRemainSpaces(garage);
            }
        }
        map.put("totalSpaces", totalSpaces);
        map.put("remainSpaces", remainSpaces);
        return map;
    }

    /**
     * 计算车场的剩余车位
     */
    @Override
    public Long getParkRemainSpaces(Long parkId) {
        ParkGarage parkGarage = new ParkGarage();
        parkGarage.setParkId(parkId);
        List<ParkGarage> garageList = parkGarageService.selectParkGarageList(SecurityUtils.getParkId(), parkGarage);
        Long remainSpaces = 0L;
        if (CollectionUtils.isNotEmpty(garageList)) {
            for (ParkGarage garage : garageList) {
                remainSpaces += getGarageRemainSpaces(garage);
            }
        }
        return remainSpaces;
    }

    @Override
    public Long getGarageTotalSpaces(ParkGarage parkGarage) {
        if (null != parkGarage) {
            Long totalSpaces = null != parkGarage.getTotalSpace() ? parkGarage.getTotalSpace() : 0L;
            // 增减车位
            Long tempSpaces = null != parkGarage.getTempEmptySpace() ? parkGarage.getTempEmptySpace() : 0L;
            return totalSpaces + tempSpaces;
        } else {
            return 0L;
        }
    }

    /**
     * 计算当前车库参与车位统计的车辆管理类型（小类）的在场车辆数
     * 增加入场状态判断，如果入场状态是禁止通行，则不计入车位统计
     */
    public Long getCountSpacesOnCarCnt(ParkGarage parkGarage) {
        if (null != parkGarage) {
            ParkOn parkOn = new ParkOn();
            parkOn.setParkId(parkGarage.getParkId());
            parkOn.setGarageId(parkGarage.getId());
            parkOn.setCountInStatus(passStatus);
            // 车库参与车位统计的类型未设置或者设置的全部
            boolean isAll;
            // 为空，则默认为全部类型
            if (StringUtils.isEmpty(parkGarage.getCountSpaceMngtype())) {
                isAll = true;
            } else {
                // 不为空，则判断是否包含全部类型
                String[] types = parkGarage.getCountSpaceMngtype().split(",");
                isAll = Arrays.asList(types).contains("0");
            }
            if (isAll) {
                parkOn.setMngtypes(null);
            } else {
                //管理类型小类，用逗号分割
                String mngTypes = parkGarage.getCountSpaceMngtype();
                parkOn.setMngtypes(mngTypes);
            }
            return parkOnService.selectParkOnCount(parkOn);
        } else {
            return 0L;
        }
    }

    /**
     * 计算当前车库剩余车位
     */
    @Override
    public Long getGarageRemainSpaces(ParkGarage parkGarage) {
        return getGarageTotalSpaces(parkGarage) - getCountSpacesOnCarCnt(parkGarage);
    }

}
